//
//  CoreDataHelper.m
//  OfficeBook
//
//  Created by 孙晓奇 on 15/9/29.
//  Copyright © 2015年 sunny. All rights reserved.
//

#import "CoreDataHelper.h"

@implementation CoreDataHelper

static CoreDataHelper *sharedInstance;

+ (instancetype)sharedInstance {
    static dispatch_once_t token;
    dispatch_once(&token, ^{
        sharedInstance = [[CoreDataHelper alloc] init];
        // init the model
        NSManagedObjectModel *managedObjectModel = [NSManagedObjectModel mergedModelFromBundles:nil];
        // create store coordinator
        NSPersistentStoreCoordinator *persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:managedObjectModel];
        // connect to the data store (on disk)
        NSString *storePath = [CoreDataHelper applicationDataDirectory];
        storePath = [storePath stringByAppendingPathComponent:@"CalendarEvent.sqlite"];
        
        NSURL *url = [NSURL fileURLWithPath:storePath];
        NSError *error;
        if (![persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:url options:nil error: &error]) {
            NSLog(@"Error creating persistent store coordinator: %@", error.localizedFailureReason);
            return;
        }
        
        // create a context and assign to the context property
        sharedInstance.context = [[NSManagedObjectContext alloc] initWithConcurrencyType:NSMainQueueConcurrencyType];
        sharedInstance.context.persistentStoreCoordinator = persistentStoreCoordinator;
    });
    
    return sharedInstance;
}

// create new object
-(NSManagedObject *)newObject{
    NSManagedObject *object = [NSEntityDescription insertNewObjectForEntityForName:sharedInstance.entityName inManagedObjectContext:sharedInstance.context];
    
    return object;
}

//delete object
-(BOOL)deleteObject:(NSManagedObject *)object{
    [self.context deleteObject:object];
    return [sharedInstance save];
}


//save
-(BOOL)save {
    NSError __autoreleasing *error;
    BOOL success = YES;
    if (![sharedInstance.context save:&error]) {
        NSLog(@"Error saving context: %@", error.localizedFailureReason);
        success = NO;
    }
    return success;
}

+ (NSString *)applicationDataDirectory
{
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    return ([paths count] > 0) ? paths[0] : nil;
}

+(BOOL)clearData {
    NSError __autoreleasing *error;
    BOOL success = YES;
    
    NSString *storePath = [CoreDataHelper applicationDataDirectory];
    storePath = [storePath stringByAppendingPathComponent:@"CalendarEvent.sqlite"];
    NSFileManager *fileManager = [NSFileManager defaultManager];
    
    BOOL blHave = [[NSFileManager defaultManager] fileExistsAtPath:storePath];
    if (!blHave) {
        return success;
    }
    if (![fileManager removeItemAtPath:storePath error:&error]) {
        NSLog(@"Error clear Data:%@", error.localizedFailureReason);
        success = NO;
    };
    return success;
}

@end
